package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.stereotype.Service;
import com.tanhua.server.vo.TodayBest;

import java.util.List;

/*
    负责和dubbo服务进行交互
 */
@Service
public class RecommendUserService {
    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;


    /**
     * 根据用户id 从MongoDB中查询佳人的方法
     * @param userId
     * @return
     */
    public TodayBest queryTodayBest(Long userId) {
        // 调用dubbo服务的查询佳人的方法
        RecommendUser recommendUser = recommendUserApi.queryWithMaxScore(userId);
        if (recommendUser == null){
            return null;
        }
        TodayBest todayBest = new TodayBest();
        todayBest.setId(recommendUser.getUserId());

        double score = Math.floor(recommendUser.getScore());
        // 先将score转换成 dubbo的封装类Dubbo 才能进行转换
        todayBest.setFateValue(Double.valueOf(score).longValue());
        // 返回今日佳人对象
        return todayBest;
    }

    /**
     *  远程调用dubbo中recommendUserApi 进行分页查询数据
     * @param id
     * @param page
     * @param pagesize
     * @return
     */
    public PageInfo<RecommendUser> queryRecommendUserList(Long id, Integer page, Integer pagesize) {
        return recommendUserApi.queryPageInfo(id, page, pagesize);
    }

    /**
     * 远程调用查询推荐好友缘分值得方法
     * @param userId        推荐用户id
     * @param toUserId      用户自己的id
     * @return
     */
    public Number queryScore(Long userId, Long toUserId) {

        return recommendUserApi.queryScore(userId,toUserId);
    }

    /**
     * 查询推荐卡片列表
     *
     * @return
     */
    public List<RecommendUser> queryCardList(Long id, int count) {
        return recommendUserApi.queryCardList(id,count);
    }
}
